"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: monitoring.py
@DateTime: 2025/07/16 00:00:00
@Docs: 监控和健康检查API
"""

from fastapi import APIRouter, status

from app.core.monitoring import (
    audit_logger,
    get_system_health,
    performance_monitor,
)
from app.schemas.base import SuccessResponse

router = APIRouter(prefix="/monitoring", tags=["监控和健康检查"])


@router.get("/health", summary="系统健康检查")
async def health_check():
    """获取系统健康状态"""
    health_status = get_system_health()
    return SuccessResponse(data=health_status, message="系统健康状态获取成功")


@router.get("/metrics", summary="性能指标")
async def get_metrics():
    """获取系统性能指标"""
    metrics = performance_monitor.get_metrics()
    return SuccessResponse(data=metrics, message="性能指标获取成功")


@router.get("/audit", summary="审计日志")
async def get_audit_logs(limit: int = 100):
    """获取审计日志"""
    logs = audit_logger.get_audit_log(limit=limit)
    return SuccessResponse(data=logs, message="审计日志获取成功")


@router.get("/audit/{entity}/{entity_id}", summary="实体审计记录")
async def get_entity_audit(entity: str, entity_id: str, limit: int = 10):
    """获取特定实体的审计记录"""
    logs = audit_logger.get_recent_operations(entity_id, limit=limit)
    return SuccessResponse(data=logs, message=f"{entity}审计记录获取成功")


@router.get("/metrics/{operation}", summary="特定操作性能指标")
async def get_operation_metrics(operation: str):
    """获取特定操作的性能指标"""
    metric = performance_monitor.get_metrics(operation)
    return SuccessResponse(data=metric, message=f"{operation}性能指标获取成功")